package autoActivity.tools;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.util.Properties;

public class LoadPropertiesUtil {
	private static Properties properties = loadProperties("info.properties");
	private static String username = getKey(properties, "chaojiyingID");
	private static String password = getKey(properties, "chaojiyingPWD");
	private static String softid = getKey(properties, "chaojiyingSOFTID");
	private static String codetype = getKey(properties, "chaojiyingCODETYPE");
	
	private static String userId = LoadPropertiesUtil.getKey(properties, "userId");
    private static String userPass = LoadPropertiesUtil.getKey(properties, "userPass");
    private static String pwd = LoadPropertiesUtil.getKey(properties, "VPNPWD");//VPN密码
    private static String email = LoadPropertiesUtil.getKey(properties, "email");
    private static int interval = Integer.parseInt(LoadPropertiesUtil.getKey(properties, "interval"));
	
    
    /**
     * 加载文件
     * @param fileName 为项目根路径下路径
     * @return
     */
    public static Properties loadProperties(String fileName) {
    	Properties properties = new Properties();
        InputStream is = null;
        try {
            String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
            String url = path + fileName;
            System.out.println("配置文件路径："+url);
            url = URLDecoder.decode(url, "UTF-8");
            is = new FileInputStream(url);
            properties.load(is);
            System.out.println("配置文件加载成功！");
        } catch (IOException e) {
        	System.out.println("配置文件加载失败！");
        }finally {
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                }
            }
        }
        return properties;
    }

    public static String getKey(Properties properties, String key){
        String result = "";
        try {
            System.out.println("读取字段："+key);
            result = (String) properties.get(key);
        } catch (Exception e) {
        	System.out.println("读取错误");
        }
        return result;
    }

	public static String getUsername() {
		return username;
	}

	public static void setUsername(String username) {
		LoadPropertiesUtil.username = username;
	}

	public static String getPassword() {
		return password;
	}

	public static void setPassword(String password) {
		LoadPropertiesUtil.password = password;
	}

	public static String getSoftid() {
		return softid;
	}

	public static void setSoftid(String softid) {
		LoadPropertiesUtil.softid = softid;
	}

	public static String getCodetype() {
		return codetype;
	}

	public static void setCodetype(String codetype) {
		LoadPropertiesUtil.codetype = codetype;
	}

	public static String getUserId() {
		return userId;
	}

	public static void setUserId(String userId) {
		LoadPropertiesUtil.userId = userId;
	}

	public static String getUserPass() {
		return userPass;
	}

	public static void setUserPass(String userPass) {
		LoadPropertiesUtil.userPass = userPass;
	}

	public static String getPwd() {
		return pwd;
	}

	public static void setPwd(String pwd) {
		LoadPropertiesUtil.pwd = pwd;
	}

	public static String getEmail() {
		return email;
	}

	public static void setEmail(String email) {
		LoadPropertiesUtil.email = email;
	}

	public static int getInterval() {
		return interval;
	}

	public static void setInterval(int interval) {
		LoadPropertiesUtil.interval = interval;
	}

}
